package com.youku.adSDK.view.components
{
   import flash.display.Loader;
   import flash.utils.Timer;
   import flash.events.TimerEvent;
   import flash.events.Event;
   import flash.events.IOErrorEvent;
   import flash.events.SecurityErrorEvent;
   import com.youku.adSDK.view.events.ADPlayerEvent;
   import flash.net.URLRequest;
   import com.youku.adSDK.model.vo.ADData;
   
   public class SWFADPlayer extends ADPlayer
   {
      
      public function SWFADPlayer() {
         super();
      }
      
      protected var _loader:Loader;
      
      protected var _swfAD:Object;
      
      protected var _loadTimer:Timer;
      
      protected var _loadTimeout:Number = 5000;
      
      protected var _adTimeout:Number = 0;
      
      private var _videoTotalTime:Number;
      
      override protected function createItems() : void {
         this._loadTimer = new Timer(this._loadTimeout);
         this._loadTimer.addEventListener(TimerEvent.TIMER,this.onLoaderTimeout);
         this._loader = new Loader();
         this._loader.visible = false;
         this.addChild(this._loader);
         createADButton();
         _layer.visible = false;
      }
      
      override protected function addEvents() : void {
         this._loader.contentLoaderInfo.addEventListener(Event.COMPLETE,this.onLoaderLoadComplete);
         this._loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onNSIOError);
         this._loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onNSSecurityError);
         this.addEventListener(Event.ENTER_FRAME,this.onThisEnterFrame);
      }
      
      protected function onLoaderTimeout(param1:TimerEvent) : void {
         this._loadTimer.stop();
         this.onNSError(ADPlayerEvent.TIME_OUT);
      }
      
      override protected function onNSError(param1:String) : void {
         _isError = true;
         this._loadTimer.stop();
         if(!_loadCompleted)
         {
            _loadCompleted = true;
            this.dispatchEvent(new ADPlayerEvent(ADPlayerEvent.LOAD_COMPLETE));
         }
         var _loc2_:ADPlayerEvent = new ADPlayerEvent(param1);
         _loc2_.id = _id;
         this.dispatchEvent(_loc2_);
      }
      
      protected function onLoaderLoadComplete(param1:Event) : void {
         var _loc2_:Object = null;
         this._loadTimer.stop();
         try
         {
            this._swfAD = this._loader.content as Object;
            this._swfAD.addEventListener("AdLoaded",this.onADLoaded);
            this._swfAD.addEventListener("AdStarted",this.onAdStarted);
            this._swfAD.addEventListener("AdStopped",this.onADStoped);
            this._swfAD.addEventListener("AdError",onNSSecurityError);
            this._swfAD.addEventListener("AdClickThru",this.onClick);
            this._swfAD.addEventListener("AdPaused",this.onAdPaused);
            this._swfAD.addEventListener("NoAd",this.onADStoped);
            this._swfAD.initAd(0,0,_width,_height);
            _loc2_ = this.creatADObject(_adData);
            this._swfAD.setAdObject(_loc2_);
            this._swfAD.load();
         }
         catch(error:Error)
         {
         }
      }
      
      protected function onADLoaded(param1:Event) : void {
         _loadCompleted = true;
         this._loadTimer.stop();
         _vw = _width;
         _vh = _height;
         var _loc2_:ADPlayerEvent = new ADPlayerEvent(ADPlayerEvent.META_DATA);
         _loc2_.totalTime = int(_adData.AL);
         this.dispatchEvent(_loc2_);
         _loadCompleted = true;
         this.dispatchEvent(new ADPlayerEvent(ADPlayerEvent.LOAD_COMPLETE));
         this.adjustWH(_width,_height);
         try
         {
            this._swfAD.setAdClickThruURL(_adData.CU);
            if(!_preLoad)
            {
               this._swfAD.startAd();
               this._loader.visible = true;
            }
         }
         catch(error:Error)
         {
         }
         if(_volume != -1)
         {
            this.setVolume(_volume);
         }
      }
      
      protected function onAdStarted(param1:Event) : void {
         this.adjustWH(_width,_height);
         _duration = _adData.AL;
         var _loc2_:ADPlayerEvent = new ADPlayerEvent(ADPlayerEvent.PLAY_START);
         this.dispatchEvent(_loc2_);
      }
      
      protected function onADStoped(param1:Event) : void {
         var _loc2_:ADPlayerEvent = new ADPlayerEvent(ADPlayerEvent.PLAY_OVER);
         this.dispatchEvent(_loc2_);
      }
      
      protected function onClick(param1:Event) : void {
         this.dispatchEvent(new ADPlayerEvent(ADPlayerEvent.SWF_CLICK));
      }
      
      protected function onAdPaused(param1:Event) : void {
         this._adTimeout = Number(_adData.AL);
      }
      
      override protected function onThisEnterFrame(param1:Event) : void {
         var _loc2_:* = 0;
         var _loc3_:Object = null;
         var _loc4_:* = NaN;
         if((_SU) && _SU.length > 0)
         {
            _loc2_ = 0;
            while(_loc2_ < _SU.length)
            {
               _loc3_ = _SU[_loc2_] as Object;
               _loc4_ = _loc3_.T;
               try
               {
                  if((this._swfAD) && (_loadCompleted))
                  {
                     if(this._swfAD.playheadTime >= _loc4_)
                     {
                        this.dispatchEvent(new ADPlayerEvent(ADPlayerEvent.ARRIVE_POINT,_loc3_.U));
                        _SU.splice(_loc2_,1);
                     }
                  }
               }
               catch(error:Error)
               {
               }
               _loc2_++;
            }
         }
      }
      
      override protected function adjustWH(param1:Number, param2:Number) : void {
         try
         {
            if((this._swfAD) && (_loadCompleted))
            {
               this._swfAD.resizeAd(param1,param2);
               this._swfAD.x = this._swfAD.y = 0;
            }
         }
         catch(error:Error)
         {
         }
         if(_layer)
         {
            _layer.width = param1;
            _layer.height = param2;
            _layer.x = 0;
            _layer.y = 0;
         }
      }
      
      override public function preload() : void {
         _preLoad = true;
         this._loader.load(new URLRequest(_adData.RS));
         this._loadTimer.start();
      }
      
      override public function play(param1:String = "") : void {
         if(_preLoad)
         {
            _preLoad = false;
            try
            {
               if(_loadCompleted)
               {
                  this._swfAD.startAd();
                  this._loader.visible = true;
               }
            }
            catch(error:Error)
            {
            }
         }
         else
         {
            this._loader.load(new URLRequest(_adData.RS));
            this._loadTimer.start();
         }
         if(_preLoad)
         {
            return;
         }
      }
      
      override public function get nowTime() : Number {
         if(!_loadCompleted)
         {
            return 0;
         }
         var _loc1_:Number = 0;
         try
         {
            _loc1_ = this._swfAD.playheadTime + this._adTimeout;
         }
         catch(error:Error)
         {
         }
         return _loc1_;
      }
      
      override public function get playStarted() : Boolean {
         return true;
      }
      
      override public function setVolume(param1:Number) : void {
         try
         {
            if((_loadCompleted) && (this._swfAD))
            {
               this._swfAD.adVolume = param1 * 0.6;
            }
            _volume = param1;
         }
         catch(error:Error)
         {
         }
      }
      
      override public function dispose() : void {
         this.unload();
         if(this.contains(_layer))
         {
            this.removeChild(_layer);
         }
         _layer.visible = false;
         this.removeEventListener(Event.ENTER_FRAME,this.onThisEnterFrame);
      }
      
      protected function unload() : void {
         try
         {
            if(this._swfAD)
            {
               this._swfAD.removeEventListener("AdLoaded",this.onADLoaded);
               this._swfAD.removeEventListener("AdStarted",this.onAdStarted);
               this._swfAD.removeEventListener("AdStopped",this.onADStoped);
               this._swfAD.removeEventListener("AdError",onNSSecurityError);
               this._swfAD.removeEventListener("AdClickThru",this.onClick);
               this._swfAD.removeEventListener("AdPaused",this.onAdPaused);
               this._swfAD.removeEventListener("NoAd",this.onADStoped);
               this._swfAD.dispose();
            }
            this._swfAD = null;
         }
         catch(e:Error)
         {
         }
         try
         {
            this._loader.unloadAndStop();
            removeChild(this._loader);
         }
         catch(e:Error)
         {
         }
      }
      
      override public function pause() : void {
         if(!_loadCompleted)
         {
            return;
         }
         if(this._swfAD)
         {
            this._swfAD.pauseAd();
         }
      }
      
      override public function resume() : void {
         if(!_loadCompleted)
         {
            return;
         }
         if(this._swfAD)
         {
            this._swfAD.resumeAd();
         }
      }
      
      override public function set id(param1:int) : void {
         _id = param1;
      }
      
      override public function get id() : int {
         return _id;
      }
      
      override public function get isError() : Boolean {
         return _isError;
      }
      
      private function creatADObject(param1:ADData) : Object {
         var _loc2_:Object = {};
         _loc2_.AT = param1.AT;
         _loc2_.RST = param1.RST;
         _loc2_.RS = param1.RS;
         _loc2_.AL = param1.AL;
         _loc2_.SU = param1.SU;
         _loc2_.SUS = param1.SUS;
         _loc2_.SUE = param1.SUE;
         _loc2_.CUM = param1.CUM;
         _loc2_.CU = param1.CU;
         _loc2_.VT = param1.VT;
         _loc2_.VC = param1.VC;
         _loc2_.W = param1.W;
         _loc2_.H = param1.H;
         _loc2_.CF = param1.CF;
         _loc2_.JS = param1.JS;
         _loc2_.BRS = param1.BRS;
         _loc2_.contentVideoDuration = this._videoTotalTime;
         _loc2_.publisherID = "youku";
         return _loc2_;
      }
      
      public function set videoTotalTime(param1:Number) : void {
         this._videoTotalTime = param1;
      }
   }
}
